package com.estronger.shopmanage.adapter.tasking;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.estronger.shopmanage.R;
import com.estronger.shopmanage.base.MyApplication;
import com.estronger.shopmanage.bean.B34BuildDetailGoods;
import com.estronger.shopmanage.manager.DataManager;
import com.estronger.shopmanage.manager.NetManager;
import com.estronger.shopmanage.utils.ConvertUtils;
import com.estronger.shopmanage.view.CustomListView;

import java.util.List;
import java.util.Map;


/**
 * Created by Soonfor on 2017/5/11.
 */

public class PgInstallDetailListAdapter extends BaseAdapter {

    private Context mContext;
    private List<B34BuildDetailGoods.BuildGoodsItem> mList;
    private boolean isDetail;

    //任务详情的适配器
    public PgInstallDetailListAdapter(Activity context, List<B34BuildDetailGoods.BuildGoodsItem> data) {
        mContext = context;
        mList = data;
        isDetail = true;
    }

    //非任务详情界面的适配器
    public PgInstallDetailListAdapter(Activity context, List<B34BuildDetailGoods.BuildGoodsItem> data, CustomListView mListView) {
        mContext = context;
        mList = data;
        isDetail = false;
        mListView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                    case MotionEvent.ACTION_MOVE:
                        v.getParent().requestDisallowInterceptTouchEvent(false);
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        v.getParent().requestDisallowInterceptTouchEvent(false);
                        break;
                }
                return true;
            }
        });
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final B34BuildDetailGoods.BuildGoodsItem mGoods = mList.get(position);
        final MsdetailHoder vh;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.layout_pg_install_affirm_listitem, null);
            vh = new MsdetailHoder(convertView);
            convertView.setTag(vh);
        } else {
            vh = (MsdetailHoder) convertView.getTag();
        }
        vh.tvfGoodsName.setText(ConvertUtils.formatNull(mGoods.goodsname));
        vh.tvfGoodsSize.setText(ConvertUtils.formatNull(mGoods.sizedesc));
        vh.tvfUnitname.setText(ConvertUtils.formatNull(mGoods.unitname));
        vh.tvfGoodsCount.setText(ConvertUtils.formatNull(mGoods.qty));
        if (mGoods.simplepicfile == null) {
        } else {
            MyApplication.imageLoader.displayImage(NetManager.getDownloadURL()
                            + "?filepath=" + mGoods.simplepicfile, vh.ivfGoodsPic,
                    MyApplication.options);
        }
        List<B34BuildDetailGoods.BuildCondition> condList = mGoods.conditionitem;
        if (DataManager.isCondMap.size() == 0) {
            DataManager.isCondMap.put(mGoods.dlvitemid, condList);
        }else{
            boolean flag = false;
            for (Map.Entry<String, List<B34BuildDetailGoods.BuildCondition>> entry : DataManager.isCondMap.entrySet()) {
                if (entry.getKey().equals(mGoods.dlvitemid)){
                    flag = true;
                    break;
                }
            }
            if (!flag) {
                DataManager.isCondMap.put(mGoods.dlvitemid, condList);
            } else {
                condList = DataManager.isCondMap.get(mGoods.dlvitemid);
            }
        }

        if(condList.size()>0) {
            vh.llfHiddenCondition.setVisibility(View.VISIBLE);
            final InstallConditonAdapter mCodAdapter = new InstallConditonAdapter(mContext, mGoods.dlvitemid, condList, isDetail, vh.cListView);
            vh.cListView.setAdapter(mCodAdapter);
            if (isDetail) {
                vh.rlfUpOrDown.setVisibility(View.GONE);
            } else {
                vh.rlfUpOrDown.setVisibility(View.VISIBLE);
                final boolean isShowChild = mGoods.isShowCondition;
                if (isShowChild) {
                    vh.llfHiddenCondition.setVisibility(View.VISIBLE);
                    vh.ivfShowConditions.setImageResource(R.drawable.pull_up_icon);
                } else {
                    vh.llfHiddenCondition.setVisibility(View.GONE);
                    vh.ivfShowConditions.setImageResource(R.drawable.pull_down_icon);
                }
                vh.rlfUpOrDown.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        if (vh.isShowing) {
                            mGoods.isShowCondition = false;
                            vh.llfHiddenCondition.setVisibility(View.GONE);
                            vh.ivfShowConditions.setImageResource(R.drawable.pull_down_icon);
                            vh.isShowing = false;
                        } else {
                            vh.llfHiddenCondition.setVisibility(View.VISIBLE);
                            vh.ivfShowConditions.setImageResource(R.drawable.pull_up_icon);
                            mGoods.isShowCondition = true;
                            vh.isShowing = true;
                        }
                        notifyDataSetChanged();
                    }
                });
            }
        }else{
            vh.rlfUpOrDown.setVisibility(View.GONE);
            vh.llfHiddenCondition.setVisibility(View.GONE);
        }
        return convertView;
    }

    class MsdetailHoder {
        TextView tvfGoodsName, tvfGoodsSize, tvfGoodsCount, tvfUnitname;
        ImageView ivfGoodsPic;
        CustomListView cListView;
        LinearLayout llfHiddenCondition;
        RelativeLayout rlfUpOrDown, rlfTou;
        ImageView ivfShowConditions;
        boolean isShowing;

        public MsdetailHoder(View view) {
            rlfTou = (RelativeLayout) view.findViewById(R.id.rlfTou);
            llfHiddenCondition = (LinearLayout) view.findViewById(R.id.llfHiddenCondition);
            rlfUpOrDown = (RelativeLayout) view.findViewById(R.id.rlfUp_Down);
            ivfShowConditions = (ImageView) view.findViewById(R.id.ivfPull);
            isShowing = false;
            tvfGoodsName = (TextView) view.findViewById(R.id.goods_name);
            tvfGoodsSize = (TextView) view.findViewById(R.id.goods_property);
            tvfGoodsCount = (TextView) view.findViewById(R.id.goods_count);
            ivfGoodsPic = (ImageView) view.findViewById(R.id.goods_item_img);
            tvfUnitname = (TextView) view.findViewById(R.id.goods_feature);
            cListView = (CustomListView) view.findViewById(R.id.lvfMsCodition);
        }
    }
}
